package com.jason.bookworm.service.impl;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jason.bookworm.dao.mapper.BorrorwRecordMapper;
import com.jason.bookworm.dao.pojo.BorrowRecord;
import com.jason.bookworm.dao.pojo.LoginUser;
import com.jason.bookworm.dao.pojo.User;
import com.jason.bookworm.service.BorrowRecordService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class BorrowRecordServiceImpl implements BorrowRecordService {

    @Resource
    private BorrorwRecordMapper borrowRecordMapper;

    /**
     * 新增借阅记录
     *
     * @param borrowRecord
     */
    @Override
    public void addNewRecord(BorrowRecord borrowRecord) {
        borrowRecordMapper.insert(borrowRecord);
    }

    @Override
    public void addNewRecord(List<BorrowRecord> borrowRecordList) {
        //添加借阅记录
        for (BorrowRecord borrowRecord : borrowRecordList) {
            borrowRecordMapper.insert(borrowRecord);
        }
    }

    /**
     * 获得该用户的所有借阅记录
     *
     * @param user
     * @return
     */
    @Override
    public List<BorrowRecord> getAllRecords(User user) {
        QueryWrapper<BorrowRecord> queryWrapper = new QueryWrapper<>();
        //借书表中username符合，且记录没有删除
        queryWrapper.eq("br_username", user.getUsername()).ne("br_status", '2');
        return borrowRecordMapper.selectList(queryWrapper);
    }

    /**
     * 获得某条记录
     *
     * @param recordId
     * @return
     */
    @Override
    public BorrowRecord getRecord(Integer recordId) {
        QueryWrapper<BorrowRecord> queryWrapper = new QueryWrapper<>();
        //根据ID查询记录
        queryWrapper.eq("br_id", recordId);
        return borrowRecordMapper.selectOne(queryWrapper);
    }

    /**
     * @param username 用户名
     * @param bookID   书籍id
     * @return 借阅记录
     */
    @Override
    public BorrowRecord getRecord(String username, Integer bookID) {
        QueryWrapper<BorrowRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("br_username", username)
                .eq("br_bookid", bookID);
        return borrowRecordMapper.selectOne(queryWrapper);
    }

    /**
     * 还书(将status设置为1)
     *
     * @param borrowRecord
     */
    @Override
    public void returnBook(BorrowRecord borrowRecord) {
        //当前时间
        Date date = new Date();
        //设置成还书状态
        borrowRecord.setStatus("1");
        //设置时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time_date = null;
        try {
            //得到当前时间想要的格式("yyyy-MM-dd")
            time_date = simpleDateFormat.parse(simpleDateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置还书时间
        borrowRecord.setReturnTime(time_date);
        //根据ID锁定记录
        QueryWrapper<BorrowRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("br_id", borrowRecord.getId());
        //更新这条记录
        borrowRecordMapper.update(borrowRecord, queryWrapper);
    }

    /**
     * 删除借阅记录(将status设置为2)
     *
     * @param borrowRecord
     */
    @Override
    public void deleteRecord(BorrowRecord borrowRecord) {
        //记录状态设置为“删除”
        borrowRecord.setStatus("2");
        //根据记录ID锁定这条记录
        QueryWrapper<BorrowRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("br_id", borrowRecord.getId());
        //更新
        borrowRecordMapper.update(borrowRecord, queryWrapper);

    }
}
